package com.companyName.sample.service.business;

import com.companyName.sample.common.exception.BusinessException;
import com.companyName.sample.data.cache.JedisCache;
import com.companyName.sample.data.entity.CityInfoExample;
import com.companyName.sample.data.entity.CityInfoPo;
import com.companyName.sample.data.mapper.CityInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Random;

/**
 * Created by guo.jun on 2016/3/29.
 */
@Component
public class CityInfoBiz extends CityInfoPo {


    @Autowired
    CityInfoMapper cityInfoMapper;

    @Autowired
    private RedisTemplate redisTemplate;


    public List<CityInfoPo> getCityInfo(CityInfoExample cityInfo) throws BusinessException {
        List<CityInfoPo> uim = cityInfoMapper.selectByExample(cityInfo);
        return uim;
    }
    public int putCityInfo(CityInfoPo cityInfo) throws BusinessException {
        Random r = new Random();
        cityInfo.setName("admin" + r.nextInt());
        cityInfo.setState("1");
        int res = cityInfoMapper.insert(cityInfo);
        return res;
    }

    public int postCityInfo(CityInfoPo cityInfo) throws BusinessException {
        int res = cityInfoMapper.updateByPrimaryKey(cityInfo);
        return res;
    }

    public int deleteCityInfo(CityInfoExample cityInfo) throws BusinessException {
        int res = cityInfoMapper.deleteByExample(cityInfo);
        return res;
    }

    public void putCityInfoForCache(CityInfoPo cityInfo) throws BusinessException {
        redisTemplate.opsForValue().set("cityInfoMapper-" + cityInfo.getName(), cityInfo);
        redisTemplate.boundValueOps("cityInfoMapper-" + cityInfo.getName()).set(cityInfo);
    }
}
